package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.property.impl.PROPERTYRIGHT;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.file.NeuronJSONTools; 
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronFunctionStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronParameterStruct;

public class NeuronFunctionLoader extends NeuronParameterStructLoader{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronFunctionLoader.class);

    @Override
    public NeuronParameterStruct toParameterStruct(IUMLNeuronAble clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("umlObj 不能为空");
        }
        if (!(clazz instanceof NeuronFunctionStruct)){
            throw new IllegalArgumentException("umlObj 必须是 NeuronFunctionStruct 类型");
        } 
        NeuronFunctionStruct clazz_struct = (NeuronFunctionStruct) clazz;
        return clazz_struct;
    }

    /**
     * 解析JSON字符串中的函数
     */
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) { 
        try {
            if (umlObj == null) {
                throw new Exception("解析函数时，指定的对象为空。");
            }
            if (json == null) {
                throw new Exception("解析函数时，指定的JSON对象为空。");
            }
            
            NeuronFunctionStruct function_struct = (NeuronFunctionStruct) toParameterStruct(umlObj);
            super.parseJSON(function_struct, json);
            
            if (json.containsKey("parameters")) {
                JSONArray _parameters = json.getJSONArray("parameters");
                if (_parameters != null) {
                    for (int i = 0; i < _parameters.size(); i++) {
                        JSONObject json_obj = _parameters.getJSONObject(i);
                        if (json_obj == null) {
                            logger.error("UML 配置文件出现错误，在第" + i + "个参数配置为空。");
                            continue;
                        }
                        IUMLNeuronAble param = UMLLoaderFactory.generateUML(json_obj);  
                        if (!(param instanceof NeuronParameterStruct)) {
                            logger.error("UML 配置文件出现错误，在第" + i + "个参数配置不是NeuronParameterStruct类型。");
                            continue;
                        }                          
                        if (param != null) {
                            function_struct.getParameters().add((NeuronParameterStruct)param);
                        }                        
                    }
                }          
            }            
        } catch(Exception e) { 
            logger.error("解析参数失败", e);
        }
    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble functionStruct) {
       JSONObject result = new JSONObject();
        try {
            if (functionStruct == null) {
                throw new Exception("要解析的UML对象为空"); 
            }
            if (!(functionStruct instanceof NeuronFunctionStruct)){
                throw new Exception("要解析的UML对象不是NeuronFunctionStruct类型"); 
            }

            result = super.toJSON(functionStruct);
            if (result == null) {
                throw new Exception("转换为JSON字符串失败"); 
            }
            
            if (((NeuronFunctionStruct)functionStruct).getParameters() != null ) {
                JSONArray _parameters = new JSONArray();
                NeuronParameterLoader paramLoader = new NeuronParameterLoader();
                for (NeuronParameterStruct param : ((NeuronFunctionStruct)functionStruct).getParameters()) {
                    JSONObject param_json_string = paramLoader.toJSON(param);
                    if (param_json_string == null) {
                        continue;
                    }
                    _parameters.add( param_json_string );
                }
                result.put("parameters", _parameters);
            } 
        } catch (Exception e) {
            logger.error("解析JSON字符串失败", e); 
             result  = new JSONObject();
        }
        return result;
    }

}
